package com.beijing.demo.find;

import java.util.Arrays;

/**
 * 曹旭
 * 2025/4/23
 * 找出有序数组中大于等于num最左的位置.
 * 人话: 找出 大于等于num的最小值,并返回坐标
 */
public class FindLeftNumber {
    public static void main(String[] args) {
        System.out.println("test start~~");
        int testTimes = 1000000;
        int len = 100;
        int value = 300;
        for (int i = 0; i < testTimes; i++) {
            int[] ranArray = ArrayUtils.getRanArray(len, value);
            int ranNum = ArrayUtils.getRanValue(value);
            Arrays.sort(ranArray);
            if(getLeftNum(ranArray,ranNum) != binaryFindNum(ranArray,ranNum)){
                System.out.println("fucking fuck~~~~");
                break;
            }
        }
        System.out.println("test end~~");
    }


    /**
     * 二分查找解法
     * 找大于等于num的最小值.
     * @param arrs
     * @param num
     * @return
     */
    public static int binaryFindNum(int[] arrs,int num){
        int resultInd = -1;
        if(arrs == null || arrs.length == 0){
            return resultInd;
        }
        int l = 0,r = arrs.length - 1;
        while(l <= r){
            int mid = l + ((r - l) >> 1);
            if(arrs[mid] < num){
                l = mid + 1;
            }else {
                resultInd = mid;
                r = mid - 1;
            }
        }
        return resultInd;
    }

    /**
     * 暴力解法
     * @param arrs
     * @param num
     * @return
     */
    public static int getLeftNum(int[] arrs,int num){
        int resultInd = -1;
        if(arrs == null || arrs.length == 0){
            return resultInd;
        }
        for (int i = 0; i < arrs.length; i++) {
            if(arrs[i] >= num){
                resultInd = i;
                break;
            }
        }
        return resultInd;
    }


}
